//
//  WelcomeViewController.swift
//  我的微博
//
//  Created by ZhangChao on 15/9/7.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupUI()
        
        // 设置用户头像
        iconImageView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl)
        
        
    }
    // 在viewWillAppear中设置动画
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        // 计算目标的约束
        let h = -(UIScreen.mainScreen().bounds.height + iconBottomCons!.constant)
        
        // 修改约束的数值
        iconBottomCons?.constant = h
        
        // 开始动画
        welcomeLabel.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
                self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                    // TODO:
                    // 发送通知
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: nil)
                })
        }
        
    }
    
    private func setupUI(){
        // 添加控件
        view.addSubview(backImageView)
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        // 设置自动布局
        
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImageView]))
        
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // 记录约束的数据
        self.iconBottomCons = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200)
        view.addConstraint(self.iconBottomCons!)

        //        也可以这样添加
//        self.iconBottomCons = view.constraints.last
        // 设定头像的宽高约束
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 90))
        
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
    }
    
    lazy var backImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    lazy var iconImageView : UIImageView = {
       
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        icon.layer.cornerRadius = 45
        icon.layer.masksToBounds = true
        
        return icon
        
    }()
    
    lazy var welcomeLabel : UILabel = UILabel(title: "欢迎归来", color: UIColor.darkGrayColor(), fontSize: 18)
}
